package com.ezcms.controller.admin;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import sun.invoke.empty.Empty;

import com.ezcms.entity.Member;
import com.ezcms.entity.PageBean;
import com.ezcms.entity.Role;
import com.ezcms.manager.MemberManager;

@Controller
public class LoginController {
	@Autowired
	private MemberManager memberManager;

	/*
	 * @RequestMapping(value = "/login/{user}", method = RequestMethod.GET)
	 * public ModelAndView myMethod(HttpServletRequest request,
	 * HttpServletResponse response, @PathVariable("user") String user, ModelMap
	 * modelMap) throws Exception { modelMap.put("loginUser", user); return new
	 * ModelAndView("/login/hello", modelMap); }
	 */
	@RequestMapping(value = "/welcome", method = RequestMethod.POST)
	public ModelAndView welcome(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap, Member user)
			throws Exception {
		List<Member> userList = (List<Member>) request.getSession()
				.getAttribute("userList");
		if (userList == null) {
			userList = new ArrayList<Member>();
		}
		userList.add(user);
		request.getSession().setAttribute("userList", userList);
		return new ModelAndView("chatRoom/main");
	}

	@RequestMapping(value = "/register", method = RequestMethod.GET)
	public ModelAndView register(Member member) {
		// System.out.println(member.getName());
		// memberManager.save(member);
		// Member m=memberManager.findMember(1);
		// System.out.println(m.getName());
		return null;
	}

	@RequestMapping(value = "/index", method = RequestMethod.GET)
	public ModelAndView index() {
		/*
		 * Member m=new Member(); m.setUsername("admin"); m.setPassword("111");
		 * memberManager.save(m);
		 */
		return new ModelAndView("index", "res", "riri");
	}

	@RequestMapping(value = "/admin/login", method = RequestMethod.GET)
	public ModelAndView login() {
		UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		 String username=userDetails.getUsername();
		 System.out.println(username);
		// Member m=new Member(); m.setName("zhf"); m.setPassword("aaa");
		// memberManager.save(m);

		return new ModelAndView("/admin/layout/main", "res", "riri");
	}

	@RequestMapping(value = "/admin/left", method = RequestMethod.GET)
	public ModelAndView left() {
		return new ModelAndView("/admin/layout/left");
	}

	@RequestMapping(value = "/admin/right", method = RequestMethod.GET)
	public ModelAndView right() {
		return new ModelAndView("/admin/layout/right");
	}

	/**
	 * 分页例子
	 * 
	 * @param request
	 * @param currentPage
	 *            当前页
	 * @return
	 */
	@RequestMapping(value = "/management/pagetest/{currentPage}")
	public ModelAndView test(HttpServletRequest request,
			@PathVariable int currentPage) {
		PageBean pageBean = (PageBean) request.getSession().getAttribute(
				"pageBean");
		if (pageBean == null) {
			pageBean = new PageBean(10, currentPage, 100);
			pageBean.setUrl(request.getContextPath() + "/management/pagetest");
		} else {
			pageBean.setCurrentPage(currentPage);
		}
		return new ModelAndView("/ftl/management/pagetest", "pageBean",
				pageBean);
	}
}
